﻿using IceDog.EFCore.Cmd.CodeFirst.Context;
using IceDog.EFCore.Models.Blog;
using IceDog.EFCore.Utils;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Text;

namespace IceDog.EFCore.Cmd.CodeFirst
{
    class Program
    {
        //https://www.cnblogs.com/chenliyang/p/6553051.html
        static void Main(string[] args)
        {
            using (var db = new DioBlogContext())
            {
                //通过sql插入
                var res = db.Set<BlogUser>().FromSql("select * from BlogUsers").ToList();
                //db.Set<BlogUser>().Select(l => l.Account).FromSql($"select name from BlogUser ").ToList();
                //db.Set<BlogUser>().FromSql("exec testproc @id", 1).ToList();
                db.Set<BlogUser>().FromSql($"select * from BlogUser where name=@name ", "tom")
                    .Select(l => new { l.Account, l.UserId }).ToList();
            }
            return;
            using(var db = new DioBlogContext())
            {
                //通过sql插入
                db.Database.ExecuteSqlCommand("insert into BlogUsers Values(4,'DHclly','www.xx.xx')");
            }
            return;
            SaveData();
            QueryData();
            Console.ReadKey();
        }
        static void SaveData()
        {
            using (var db = new DioBlogContext())
            {
                var userIsExist = db.BlogUsers.Any(u => u.Account == "晓晨master");
                if (userIsExist) { return; }
                var user = new BlogUser()
                {
                    Account = "晓晨master",
                    BlogUrl = "www.example.com/xiaochenmaster",
                };
                db.BlogUsers.Add(user);
                var count = db.SaveChanges();
                Console.WriteLine("{0} records saved to database", count);
                var article = new BlogArticle
                {
                    Author = user,
                    CreateTime = DateTime.Now,
                    Title = "EF Core 入门系列 1",
                    Content = "入门。。。。"
                };
                db.BlogArticles.Add(article);
                count = db.SaveChanges();
                Console.WriteLine("{0} records saved to database", count);
                var comment = new BlogComment()
                {
                    CommentTime = DateTime.Now,
                    Content = "大佬厉害",
                    Article = article,
                    Commenter = user
                };
                db.BlogComments.Add(comment);
                count = db.SaveChanges();
                Console.WriteLine("{0} records saved to database", count);
            }
        }
        static void QueryData()
        {
            //查询
            using (var db = new DioBlogContext())
            {
                var user = db.BlogUsers
                    .Include(u=>u.Articles)
                    .AsNoTracking()
                    .FirstOrDefault(u => u.Account == "晓晨master");
                foreach (var article in db.BlogUsers)
                {

                }
                if (user != null)
                {
                    var settings = new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    };
                    Console.WriteLine("晓晨master的博客数据\r\n" + JsonConvert.SerializeObject(user, Formatting.Indented, settings));
                }
            }
        }
    }
}
